package com.md.api.controller;

import com.md.api.annotation.NoRepeatSubmit;
import com.md.api.common.config.RedisKeys;
import com.md.api.common.utils.RedisUtil;
import com.md.api.common.utils.StringUtils;
import com.md.api.entity.*;
import com.md.api.entity.dto.ComposeCommitDto;
import com.md.api.entity.dto.ComposeQueryDto;
import com.md.api.service.ProductService;
import com.md.api.service.SysUserSevice;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@RequestMapping("/sys")
@RestController
public class SysController {


	@Resource
	ProductService productSevice;
	@Resource
	SysUserSevice sysUserSevice;
	@Resource
	RedisUtil redisUtil;

	@RequestMapping("/index/count")
	@ResponseBody
	public HttpResult userPage() {

		return sysUserSevice.indexCount();
	}
	@RequestMapping("/userPage")
	@ResponseBody
	public HttpResult userPage( @RequestBody ComposeQueryDto composeQueryDto) {
		
		return sysUserSevice.userPage(composeQueryDto);
	}
	@RequestMapping("/deleteUser")
	@ResponseBody
	public HttpResult deleteUser( @RequestBody ComposeCommitDto commitDto) {
	
		return sysUserSevice.deleteUser(commitDto);
	}
	@RequestMapping("/resetPass")
	@ResponseBody
	public HttpResult resetPass( @RequestBody SysUser sysUser) {
		
		return sysUserSevice.resetPass(sysUser);
	}

	@RequestMapping("/enableUser")
	@ResponseBody
	public HttpResult enableUser( @RequestBody SysUser user) {
	
		return sysUserSevice.enableUser(user);
	}

	@RequestMapping("/addUser")
	@ResponseBody
	public HttpResult register( @RequestBody SysUser user) {
	
		return sysUserSevice.addUser(user);
	}

	@RequestMapping("/updateUser")
	@ResponseBody
	public HttpResult updateUser( @RequestBody SysUser user) {
	
		return sysUserSevice.updateUser(user);
	}

	@RequestMapping("/updatePass")
	@ResponseBody
	public HttpResult updatePass(HttpServletRequest request, @RequestBody SysUser user) {
		SysUser dbUser = (SysUser)redisUtil.get(RedisKeys.getSysUserTokenInfoKey(request.getHeader("token")));
		user.setUserId(dbUser.getUserId());
		return sysUserSevice.updatePass(user);
	}




	@RequestMapping("/login")
	public HttpResult login( @RequestBody SysUser user) {
		return sysUserSevice.sysUserLogin(user);
	}

	@RequestMapping("/evaluationPage")
	public HttpResult evaluationPage(@RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.evaluationPage(composeQueryDto);
	}
	@RequestMapping("/addEvaluation")
	public HttpResult addEvaluation(@RequestBody EvaluationRecord record) {

		return sysUserSevice.addEvaluation(record);
	}

	@RequestMapping("/deleteEvaluation")
	public HttpResult deleteEvaluation(@RequestBody EvaluationRecord record) {
		if (null==record.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.deleteEvaluation(record);
	}

	@RequestMapping("/updateEvaluation")
	public HttpResult updateEvaluation(@RequestBody EvaluationRecord record) {
		if (null==record.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.updateEvaluation(record);
	}


	@RequestMapping("/phoneCodePage")
	public HttpResult phoneCodePage(@RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.phoneCodePage(composeQueryDto);
	}
	@RequestMapping("/addPhoneCode")
	public HttpResult addPhoneCode(@RequestBody PhoneCodeConfirmLaw record) {

		return sysUserSevice.addPhoneCode(record);
	}

	@RequestMapping("/deletePhoneCode")
	public HttpResult deletePhoneCode(@RequestBody PhoneCodeConfirmLaw record) {
		if (null==record.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.deletePhoneCode(record);
	}

	@RequestMapping("/updatePhoneCode")
	public HttpResult updatePhoneCode(@RequestBody PhoneCodeConfirmLaw record) {
		if (null==record.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.updatePhoneCode(record);
	}



	@RequestMapping("/newAttribute")
	public HttpResult newAttribute(@RequestBody PdAttribute attribute) {

		return productSevice.newAttribute(attribute);
	}

	@RequestMapping("/deleteAttribute")
	public HttpResult deleteAttribute(@RequestBody PdAttribute attribute) {
		return productSevice.deleteAttribute(attribute);
	}

	@RequestMapping("/attributeList")
	public HttpResult attributeList(@RequestBody PdAttribute attribute) {
		return productSevice.attributeList(attribute);
	}

	@RequestMapping("/attributeTree")
	public HttpResult attributeTree(@RequestBody PdAttribute attribute) {
		return productSevice.attributeTree(attribute);
	}

	@RequestMapping("/newAttributeValue")
	public HttpResult newAttribute(@RequestBody PdAttributeValue attributeValue) {
		return productSevice.newAttributeValue(attributeValue);
	}

	@RequestMapping("/deleteAttributeValue")
	public HttpResult deleteAttributeValue(@RequestBody PdAttributeValue attributeValue) {
		return productSevice.deleteAttributeValue(attributeValue);
	}

	@RequestMapping("/attributeValueList")
	public HttpResult attributeValueList(@RequestBody PdAttributeValue attributeValue) {
		if (StringUtils.isEmpty(attributeValue.getAttributeId())){
			return HttpResult.PARAM_ERROR;
		}
		return productSevice.attributeValueList(attributeValue.getAttributeId());
	}

	@NoRepeatSubmit
	@RequestMapping("/newProduct")
	public HttpResult newProduct(@RequestBody PdSpu spu) {
		if (null==spu.getType()||StringUtils.isEmpty(spu.getProductName())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			HttpResult result = productSevice.newProduct( spu);
			return result;
		}catch (Exception e){
			e.printStackTrace();
            return HttpResult.fail("新增商品失败！");
		}
	}

	//商品列表
	@RequestMapping("/spuPage")
	public HttpResult spuPage(@RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.spuPage(composeQueryDto);
	}

	//删除产品
	@RequestMapping("/deleteSpu")
	public HttpResult deleteSpu(@RequestBody ComposeQueryDto composeQueryDto) {
		if (StringUtils.isEmpty(composeQueryDto.getSpuId())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.deleteSpu(composeQueryDto);
	}

	//产品列表
	@RequestMapping("/skuPage")
	public HttpResult skuPage(@RequestBody ComposeQueryDto composeQueryDto) {
		if (StringUtils.isEmpty(composeQueryDto.getSpuId())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.skuPage(composeQueryDto);
	}
	//产品的上架
	@NoRepeatSubmit
	@RequestMapping("/skuUp")
	public HttpResult skuUp(@RequestBody PdSku sku) {
		if (StringUtils.isEmpty(sku.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			return productSevice.skuUp( sku);
		}catch (Exception e){
			e.printStackTrace();
			return HttpResult.fail("產品上架失敗！");
		}
	}
	//产品的下架
	@NoRepeatSubmit
	@RequestMapping("/skuDown")
	public HttpResult skuDown(@RequestBody PdSku sku) {
		if (StringUtils.isEmpty(sku.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			return productSevice.skuDown( sku);
		}catch (Exception e){
			e.printStackTrace();
			return HttpResult.fail("產品下架失敗！");
		}
	}

	//修改商品的基本信息
	@NoRepeatSubmit
	@RequestMapping("/spu/updateBase")
	public HttpResult updateSpuBase(@RequestBody PdSpu spu) {
		if (StringUtils.isEmpty(spu.getSpuId())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			return productSevice.updateSpuBase(spu);
		}catch (Exception e){
			e.printStackTrace();
			return HttpResult.fail("商品修改失敗！");
		}
	}

	//修改产品的基本信息
	@NoRepeatSubmit
	@RequestMapping("/sku/updateBase")
	public HttpResult updateBase(@RequestBody PdSku sku) {
		if (StringUtils.isEmpty(sku.getSkuId())){
			return HttpResult.PARAM_ERROR;
		}
		try {
			return productSevice.updateBase(sku);
		}catch (Exception e){
			e.printStackTrace();
			return HttpResult.fail("產品修改失敗！");
		}
	}

	@RequestMapping("/orderPage")
	@ResponseBody
	public HttpResult orderPage( @RequestBody ComposeQueryDto composeQueryDto) {
		
		return sysUserSevice.orderPage(composeQueryDto);
	}

	@RequestMapping("/distributeOrder")
	@ResponseBody
	public HttpResult distributeOrder( @RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.distributeOrder(composeQueryDto);
	}


	@RequestMapping("/vipOrderDetail")
	@ResponseBody
	public HttpResult vipOrderDetail( @RequestBody TbOrder order) {
		if (StringUtils.isEmpty(order.getOrderNo())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.vipOrderDetail(order);
	}

	@RequestMapping("/productOrderDetail")
	@ResponseBody
	public HttpResult productOrderDetail( @RequestBody  TbOrder order) {
		if (StringUtils.isEmpty(order.getOrderNo())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.productOrderDetail(order);
	}


	@RequestMapping("/customPage")
	@ResponseBody
	public HttpResult customPage( @RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.customPage(composeQueryDto);
	}

	//解锁用户
	@RequestMapping("/unlockCustom")
	@ResponseBody
	public HttpResult unlockCustom( @RequestBody UserInfo userInfo) {

		return sysUserSevice.unlockCustom(userInfo);
	}



	@RequestMapping("/vipPage")
	@ResponseBody
	public HttpResult vipPage( @RequestBody ComposeQueryDto composeQueryDto) {

		return sysUserSevice.vipPage(composeQueryDto);
	}
	@RequestMapping("/newVip")
	@ResponseBody
	public HttpResult newVip( @RequestBody VipProperties vipProperties) {

		return sysUserSevice.newVip(vipProperties);
	}
	@RequestMapping("/setVip")
	@ResponseBody
	public HttpResult setVip( @RequestBody VipProperties vipProperties) {

		return sysUserSevice.setVip(vipProperties);
	}


	@RequestMapping("/photoPage")
	@ResponseBody
	public HttpResult page( @RequestBody ComposeQueryDto qto) {
		return sysUserSevice.photoPage(qto);
	}

	@RequestMapping("/picturePage")
	@ResponseBody
	public HttpResult picturePage(@RequestBody ComposeQueryDto qto) {
		return sysUserSevice.picturePage(qto);
	}

	@RequestMapping("/deletePicture")
	@ResponseBody
	public HttpResult deletePicture(@RequestBody PictureInfo picture) {
		return sysUserSevice.deletePicture(picture);
	}

	@NoRepeatSubmit
	@RequestMapping("/thawingPhoto")
	@ResponseBody
	public HttpResult thawingPhoto(@RequestBody PhotoInfo photoInfo) {
		if (StringUtils.isEmpty(photoInfo.getPhotoId())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.thawingPhoto(photoInfo);
	}


	@RequestMapping("/switchList")
	public HttpResult switchList() {
		return sysUserSevice.switchList();
	}

	@NoRepeatSubmit
	@RequestMapping("/switchOpen")
	public HttpResult switchOpen(@RequestBody SysSwitch sysSwitch) {
		return sysUserSevice.switchOpen(sysSwitch);
	}
	@NoRepeatSubmit
	@RequestMapping("/switchClose")
	public HttpResult switchClose(@RequestBody SysSwitch sysSwitch) {
		return sysUserSevice.switchClose(sysSwitch);
	}


	@RequestMapping("/picShow")
	public HttpResult picShow() {
		return sysUserSevice.picShow();
	}


	@RequestMapping("/setPicShow")
	public HttpResult setPicShow(@RequestBody SysPcConfiguration pcConfiguration) {
		if (StringUtils.isEmpty(pcConfiguration.getCode())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.setPicShow(pcConfiguration);
	}

	@RequestMapping("/docInstruction")
	@ResponseBody
	public HttpResult docInstruction(HttpServletRequest request, @RequestBody  SysPcDocInstruction instruction) {
		if (StringUtils.isEmpty(instruction.getCode())){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.docInstruction(instruction);
	}


	@RequestMapping("/setDocInstruction")
	public HttpResult setDocInstruction(@RequestBody SysPcDocInstruction pcConfiguration) {
		if (null==pcConfiguration.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.setDocShow(pcConfiguration);
	}

	@RequestMapping("/newDoc")
	public HttpResult newDoc(@RequestBody SysPcDocInstruction pcConfiguration) {

		return sysUserSevice.newDoc(pcConfiguration);
	}

	@RequestMapping("/delDoc")
	public HttpResult delDoc(@RequestBody SysPcDocInstruction pcConfiguration) {
		if (null==pcConfiguration.getId()){
			return HttpResult.PARAM_ERROR;
		}
		return sysUserSevice.delDoc(pcConfiguration);
	}
}
